/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean.chart.iterfaces;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JPanel;

/**
 *
 * @author Lukasz
 */
public abstract class AbstractChart extends JPanel implements AbstractChartModel, 
        AbstractChartView, Serializable{

    protected final String UNIT_PARAM_NAME = "Jednostka";
    protected final String MAXYVALUE_PARAM_NAME = "Wartość Y";
    protected final String COUNTVALUE_PARAM_NAME = "Ilośc punktów na wykresie";
    protected final String DOTSIZE_PARAM_NAME = "Wielkośc punktora";
    protected final String STROKESIZE_PARAM_NAME = "Grubość lini";
    protected final String LEGEND_PARAM_NAME = "Legenda";
    protected final String RUNNING_PARAM_NAME = "Pobieranie wartości";
    protected final String LINEVISIBLE_PARAM_NAME = "Widoczność linii";
    protected final String DOTVISIBLE_PARAM_NAME = "Widoczność punktów";
    protected final String GRIDVISIBLE_PARAM_NAME = "Widoczność siatki";
    protected final String VALUEVISIBLE_PARAM_NAME = "Widoczność wawrtości";
    protected final String CHARTCOLOR_PARAM_NAME = "Tablica kolorów";
    protected final String CHARTCOLORINDEX_PARAM_NAME = "Wartość koloru dla indeksu";
    protected final String BCKGCOLOR_PARAM_NAME = "Kolor tła";
    protected final String GRIDCOLOR_PARAM_NAME = "Kolor siatki";
    protected final String DOTCOLOR_PARAM_NAME = "Kolor punktów";
    protected final String VALUECOLOR_PARAM_NAME = "Kolor wartości";
    protected final String STEPMARKSCOUNT_PARAM_NAME = "Liczba zaznaczeń na osi X";
    
    
    private double maxYValue = 10;
    private int countValue = 5;
    private int dotSize = 10;
    private int strokeSize = 1;
    private int markCount = 5;
    

    protected HashMap<String, List<Double>> chartMap = new HashMap<>();
    protected HashMap<String, Color> chartColorMap = new HashMap<>();

    private String unit = "J";
    private String legend = "Legenda";
    
    private boolean running = true;
    private boolean lineVisible = true;
    private boolean dotVisible = true;
    private boolean gridVisible = true;
    private boolean valueVisible = true;
    
    private Color[] chartColor;
    private Color bckgColor = Color.WHITE;
    private Color gridColor = Color.LIGHT_GRAY;
    private Color valueColor = Color.BLACK;
    
    //protected PropertyChangeSupport propertySupport = new PropertyChangeSupport(this);
    
    @Override
    public void addChartData(String chartName) {
        chartMap.put(chartName, new LinkedList<Double>());
    }

    @Override
    public List<Double> getChartData(String chartName) {
        return chartMap.get(chartName);
    }

    @Override
    public void addChartDataPoint(String chartName, Double value) {
        List<Double> list = getChartData(chartName);
        if(getMaxYValue() < value)
            setMaxYValue(value);
        list.add(value);
    }

    @Override
    public void setMaxYValue(double maxYValue) {
        double oldValue = this.maxYValue;
        this.maxYValue = maxYValue;
        repaint();
        //propertySupport.firePropertyChange(MAXYVALUE_PARAM_NAME, oldValue, maxYValue);
        propertyChange(MAXYVALUE_PARAM_NAME, oldValue, maxYValue);
    }

    @Override
    public double getMaxYValue() {
        return maxYValue;
    }

    @Override
    public void setUnit(String unit) {
        String oldUnit = this.unit;
        this.unit = unit;
        repaint();
        //propertySupport.firePropertyChange(UNIT_PARAM_NAME, oldUnit, unit);
        propertyChange(UNIT_PARAM_NAME, oldUnit, unit);
    }

    @Override
    public String getUnit() {
        return unit;
    }

    @Override
    public void setLegendLabel(String label) {
        String oldLabel = legend;
        legend = label;
        repaint();
        //propertySupport.firePropertyChange(LEGEND_PARAM_NAME, oldLabel, label);
        propertyChange(LEGEND_PARAM_NAME, oldLabel, label);
    }

    @Override
    public String getLegendLabel() {
        return legend;
    }

    @Override
    public void setRunning(boolean running) {
        boolean oldRunning = this.running;
        this.running = running;
        //propertySupport.firePropertyChange(RUNNING_PARAM_NAME, oldRunning, running);
        propertyChange(RUNNING_PARAM_NAME, oldRunning, running);
    }

    @Override
    public boolean isRunning() {
        return running;
    }

    @Override
    public void setLineVisible(boolean visible) {
        boolean oldLineVisible = this.lineVisible;
        lineVisible = visible;
        //propertySupport.firePropertyChange(LINEVISIBLE_PARAM_NAME, oldLineVisible, lineVisible);
        propertyChange(LINEVISIBLE_PARAM_NAME, oldLineVisible, lineVisible);
    }

    @Override
    public boolean isLineVisible() {
        return lineVisible;
    }

    @Override
    public void setDotVisible(boolean visible) {
        boolean oldDotVisible = this.dotVisible;
        dotVisible = visible;
        //propertySupport.firePropertyChange(DOTVISIBLE_PARAM_NAME, oldDotVisible, visible);
        propertyChange(DOTVISIBLE_PARAM_NAME, oldDotVisible, visible);
    }

    @Override
    public boolean isDotVisible() {
        return dotVisible;
    }

    @Override
    public void setGridVisible(boolean visible) {
        boolean oldGridVisible = this.gridVisible;
        gridVisible = visible;
        //propertySupport.firePropertyChange(GRIDVISIBLE_PARAM_NAME, oldGridVisible, visible);
        propertyChange(GRIDVISIBLE_PARAM_NAME, oldGridVisible, visible);
        
    }

    @Override
    public boolean isGridVisible() {
        return  gridVisible;
    }

    @Override
    public void setColor(String name, Color color) {
        chartColorMap.put(name, color);
    }

    @Override
    public Color getChartColor(String name) {
        return chartColorMap.get(name);
    }

    @Override
    public void setChartColor(Color[] color) {
        Color[] oldChartColor = this.chartColor;
        chartColor = color;
        //propertySupport.firePropertyChange(CHARTCOLOR_PARAM_NAME, oldChartColor, color);
        propertyChange(CHARTCOLOR_PARAM_NAME, oldChartColor, color);
    }

    @Override
    public Color[] getChartColor() {
        return chartColor;
    }

    @Override
    public void setChartColor(int i, Color color) {
        Color oldChartColorIndex = this.chartColor[i];
        chartColor[i] = color;
        //propertySupport.firePropertyChange(CHARTCOLORINDEX_PARAM_NAME, oldChartColorIndex, color);
        propertyChange(CHARTCOLORINDEX_PARAM_NAME, oldChartColorIndex, color);
    }

    @Override
    public Color getChartColor(int i) {
        return chartColor[i];
    }

    @Override
    public void setBackgroundColor(Color color) {
        Color oldBckgColor = this.bckgColor;
        bckgColor = color;
        //propertySupport.firePropertyChange(BCKGCOLOR_PARAM_NAME, oldBckgColor, color);
        propertyChange(BCKGCOLOR_PARAM_NAME, oldBckgColor, color);
    }

    @Override
    public Color getBackgroundColor() {
        return bckgColor;
    }

    @Override
    public void setGridColor(Color color) {
        Color oldGridColor = this.gridColor;
        gridColor = color;
        repaint();
        //propertySupport.firePropertyChange(GRIDCOLOR_PARAM_NAME, oldGridColor, color);
        propertyChange(GRIDCOLOR_PARAM_NAME, oldGridColor, color);
    }

    @Override
    public Color getGridColor() {
        return gridColor;
    }

    @Override
    public void setValuesCount(int count) {
        int oldCountValue = this.countValue;
        countValue = count;
        //propertySupport.firePropertyChange(COUNTVALUE_PARAM_NAME, oldCountValue, count);
        propertyChange(COUNTVALUE_PARAM_NAME, oldCountValue, count);
    }

    @Override
    public int getValuesCount() {
        return countValue;
    }

    @Override
    public void setDotSize(int size) {
        int oldDotSize = this.dotSize;
        dotSize = size;
        //propertySupport.firePropertyChange(DOTSIZE_PARAM_NAME, oldDotSize, size);
        propertyChange(DOTSIZE_PARAM_NAME, oldDotSize, size);
    }

    @Override
    public int getDotSize() {
        return dotSize;
    }
    @Override
    public void setMarksCount(int count){
        int oldCount = markCount;
        markCount = count;
        propertyChange(STEPMARKSCOUNT_PARAM_NAME, oldCount, markCount);
    }
    @Override
    public int getMarksCount(){
        return markCount;
    }
    @Override
    public void setValueVisible(boolean  visible){
         boolean oldValueVisible = this.valueVisible;
        valueVisible = visible;
        //propertySupport.firePropertyChange(GRIDVISIBLE_PARAM_NAME, oldGridVisible, visible);
        propertyChange(VALUEVISIBLE_PARAM_NAME, oldValueVisible, visible);
    }
    @Override
    public boolean isValueVisible()
    {
        return valueVisible;
    }
    @Override
    public void setValueColor(Color color){
        Color oldValueColor = this.valueColor;
        valueColor = color;
        repaint();
        propertyChange(VALUECOLOR_PARAM_NAME, oldValueColor, color);
    }
    @Override
    public Color getValueColor(){
        return valueColor;
    }
    
    @Override
    public void setStrokeSize(int size){
        int oldValue = this.strokeSize;
        this.strokeSize = size;
        repaint();
      
        propertyChange(STROKESIZE_PARAM_NAME, oldValue, size);
    }
    @Override
    public int getStrokeSize(){
        return strokeSize;
    }
    
    @Override
    public abstract void paintComponent(Graphics g);
    
    protected abstract void paintGrid(Graphics g);
    protected abstract void paintLine(Graphics2D g2, int w, int h);
    protected abstract void paintDot(Graphics2D g2, int w, int h);
    protected abstract void paintValue(Graphics g, int h);
    protected abstract void propertyChange(String property,int oldval, int newVal);
    protected abstract void propertyChange(String property,Object oldval, Object newVal);
    protected abstract void propertyChange(String property,double oldval, double newVal);
    protected abstract void propertyChange(String property,boolean oldval, boolean newVal);
   
}
